import $ from 'jquery'
import Vue from 'vue';
import bus from '@/js/common/bus.js';
import tool from '@/js/plugins/zry.plugins.tool.js';
export default {
		rootUrl: 'http://www.51bijiawang.cn:8391/', //服务器
		ftpUrl: 'http://www.51bijiawang.cn:8391/', //服务器
	post(url, data,reject_) {
		if(isDoubleClick(url, data))
			return;
		if(!data) {
			data = {};
		}
		let params = {};
		params.token = tool.getToken();

		params.device = "device_web";
		if(data.pageNum > 0) {
			params.page = {
				pageNum: data.pageNum,
				pageSize: data.pageSize
			};
		}
		let clone_data = tool.cloneObj(data)
		delete clone_data.pageNum;
		delete clone_data.pageSize;
		params.params = clone_data;

		let requesturl = this.rootUrl + url;
			
		return new Promise((resolve, reject) => {
			$.ajax({
				url: requesturl,
				data: JSON.stringify(params),
				type: "POST",
				async: true,
				dataType: 'json',
				contentType: "application/json",
				success: function(res) {
					bus.$emit('message', res);
					bus.$emit('isLoadings', false);
					if(res.code == 1 || res.code == 131) {
						resolve(res);
					}else{
						reject_(res);
					}
					
					
					
				},
				error: function(request, textStatus, errorThrown) {
					reject();
					if(errorback)
						errorback({
							msg: '网络异常或系统错误！'
						});
				}
			});
		})
	},
	ajaxPost: function(url, data, successback, errerback) {
		if(isDoubleClick(url, data))
			return;
		if(this.rootUrl.indexOf('localhost:8070') > -1) {
			this.ajax(url, data, "POST", true, successback, errerback);
		} else {
			this.ajax2(url, data, "POST", true, successback, errerback);
		}
	},
	ajax(requesturl, data, type, isasync, successback, errorback) {
		let params_;
		if(data && data.pageNum) {
			data.page = {
				pageNum: 0,
				pageSize: 10
			}
		}
		if(typeof data == 'object') {
			params_ = JSON.stringify(data)
		} else {
			params_ = data;
		}
		let params = {
			url: requesturl,
			data: params_,
			device: "device_web"
		}
		let requestUrl = this.rootUrl + 'admin';
		baseAjax(requestUrl, type, isasync, params, successback, errorback);
	},
	ajax2(requesturl, data, type, isasync, successback, errorback) {
		if(!data) {
			data = {};
		}
		let params = {};
		params.token = tool.getToken();

		params.device = "device_web";
		if(data.pageNum > 0) {
			params.page = {
				pageNum: data.pageNum,
				pageSize: data.pageSize
			};
		}
		let clone_data = tool.cloneObj(data)
		delete clone_data.pageNum;
		delete clone_data.pageSize;
		params.params = clone_data;

		/**********测试专用代码 begin**********/
		//		if(requesturl == 'dict/getListValue'){
		//			requesturl = 'http://119.23.172.36:8391/' + requesturl;
		//		}else{
		//			requesturl = this.rootUrl + requesturl;
		//		}
		/**********测试专用代码 end**********/

		requesturl = this.rootUrl + requesturl;
		baseAjax(requesturl, type, isasync, JSON.stringify(params), successback, errorback);
	},
	ajaxFilePost: function(url, data, successback, errerback) {
		FilePost(this.rootUrl + url, data, "POST", successback, errerback);
	},
	ajaxAsyncBaseGetFullUrl: function(url, data, successback, errerback) {
		ajax3(url, data, "Get", true, successback, errerback);
	},
};

function baseAjax(requestUrl, type, isasync, params, successback, errorback) {
	if(params.page && params.page.pageNum && params.page.pageSize) {
		bus.$emit('isLoadings', true);
	}
	$.ajax({
		url: requestUrl,
		data: params,
		type: type,
		async: isasync,
		dataType: 'json',
		contentType: "application/json",
		success: function(res) {
			bus.$emit('message', res);
			bus.$emit('isLoadings', false);
			if(res.code == 1 || res.code == 131) {
				if(successback)
					successback(res);
			} else {
				if(errorback)
					errorback(res);
			}
		},
		error: function(request, textStatus, errorThrown) {
			if(errorback)
				errorback({
					msg: '网络异常或系统错误！'
				});
		}
	});
}

function isDoubleClick(url, param) { //防止重复提交
	let before_time = sessionStorage.getItem('ajax_time');
	let before_url = sessionStorage.getItem('ajax_url');
	let before_params = sessionStorage.getItem('ajax_params')
	if(!(before_time && before_url && before_params)) {
		sessionStorage.setItem('ajax_time', new Date().valueOf())
		tool.setStorage('ajax_url', url)
		tool.setStorage('ajax_params', param)
		return false;
	}
	let now_time = new Date().valueOf();
	let interval = now_time - before_time;
	let isUrlEqual = before_url == url ? true : false;
	let isParamsEqual = before_params == param ? true : false;
	sessionStorage.setItem('ajax_time', new Date().valueOf())
	tool.setStorage('ajax_url', url)
	tool.setStorage('ajax_params', param)
	if(interval > 80 && interval < 350 && isUrlEqual && isParamsEqual) {
		return true;
	}
	return false;
}

function FilePost(requesturl, data, type, successback, errorback) {
	bus.$emit('isLoadings', true);
	data.append('name', 111)
	//	debugger
	setTimeout(() => {
		$.ajax({
			type: 'POST',
			url: requesturl,
			//			async: false,
			dataType: 'json',
			crossDomain: true,
			data: data,
			processData: false,
			contentType: false,
			success: function(res) {
				bus.$emit('isLoadings', false);
				if(res.code == 1) {
					if(successback)
						successback(res);
				} else {
					if(errorback)
						errorback(res);
				}
			},
			error: function(request, textStatus, errorThrown) {
				if(errorback)
					errorback(request, textStatus, errorThrown);
			},
			beforeSend: function(xhr) {

			}
		});
	}, 0)

}

function ajax3(requesturl, data, type, isasync, successback, errorback) {
	if(typeof data !== 'string') {
		data = JSON.stringify(data);
	}
	if(!type) {
		type = "POST";
	}
	$.ajax({
		xhrFields: {
			withCredentials: true
		},
		dataType: "jsonp",
		type: type,
		url: requesturl,
		success: function(res) {
			if(successback)
				successback(res);
		},
		error: function(request, textStatus, errorThrown) {
			if(errorback)
				errorback(request, textStatus, errorThrown);
		}
	});
}